/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Lucas
 */

/* -------------------
* Chart.java
* -------------------
* (C) Copyright 2002-2005, by Object Refinery Limited.
*
*/
package demo;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RefineryUtilities;
/**
* A simple demonstration application showing how to create a line chart using
* data from a {@link CategoryDataset}.
*/

public class Chart extends ApplicationFrame{
    public Chart(String title) {
        super(title);
        super.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        CategoryDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);
    }
    
    public Chart(String title, ResultSet rs){
        super(title);
        super.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        CategoryDataset dataset = createDataset(rs);
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);
    }
     
    /**
    * Creates a sample dataset.
    *
    * @return The dataset.
    */
       
    private static CategoryDataset createDataset(ResultSet rs) {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        try {
            rs.first();
            while (rs.next()) {    
                /*
                rs.getString("DIA_LEITURA");
                rs.getString("SITUAC");
                rs.getString("SMS");
                */
                /*
                if(Integer.parseInt(rs.getString("VALOR")) >700 ||
                   Integer.parseInt(rs.getString("VALOR")) < 10){
                        continue;
                }
                */
                dataset.addValue(Integer.parseInt(rs.getString("VALOR")), 
                                rs.getString("IDSENSOR"), 
                                rs.getString("HORA_LEITURA"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(Chart.class.getName()).log(Level.SEVERE, null, ex);
        }
        return dataset;
    }
    
    private static CategoryDataset createDataset() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(212, "Classes", "JDK 1.0");
        dataset.addValue(504, "Classes", "JDK 1.1");
        dataset.addValue(1520, "Classes", "SDK 1.2");
        dataset.addValue(1842, "Classes", "SDK 1.3");
        dataset.addValue(2991, "Classes", "SDK 1.4");
        return dataset;
    }
    /**
    * Creates a sample chart.
    *
    * @param dataset a dataset.
    *
    * @return The chart.
    */
    private static JFreeChart createChart(CategoryDataset dataset) {
    // create the chart...
        JFreeChart chart = ChartFactory.createLineChart(
            "Variação na leitura dos sensores", // chart title
            "Data/hora da leitura", // domain axis label
            "Valor de leitura", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            false, // include legend
            true, // tooltips
            false // urls
        );
        chart.addSubtitle(new TextTitle(""));
        TextTitle source = new TextTitle("");
        source.setFont(new Font("SansSerif", Font.PLAIN, 10));
        source.setPosition(RectangleEdge.BOTTOM);
        source.setHorizontalAlignment(HorizontalAlignment.RIGHT);
        chart.addSubtitle(source);
        chart.setBackgroundPaint(Color.white);
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setRangeGridlinePaint(Color.white);
        // customise the range axis...
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        // customise the renderer...
        LineAndShapeRenderer renderer
        = (LineAndShapeRenderer) plot.getRenderer();
        renderer.setShapesVisible(true);
        renderer.setDrawOutlines(true);
        renderer.setUseFillPaint(true);
        renderer.setFillPaint(Color.white);
        return chart;
    }
    /**
    * Creates a panel for the demo (used by SuperDemo.java).
    *
    * @return A panel.
    */
    public static JPanel createDemoPanel() {
        JFreeChart chart = createChart(createDataset());
        return new ChartPanel(chart);
    }
    /**
    * Starting point for the demonstration application.
    *
    * @param args ignored.
    */
    public static void main(String[] args) {
        Chart demo = new Chart("Line Chart Demo");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }
}
